﻿/*
 * Copyright (c) 2015-2016 Constantijn Evenhuis (blueprints@const.nl)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
using Blueprints.Resolution;
using System;
using System.Collections.Generic;

namespace Blueprints.Defaults
{
    internal sealed class TypeResolverCache : ITypeResolver
    {
        private readonly Dictionary<Type, TypeCategory> typeCategories = new Dictionary<Type, TypeCategory>();
        private readonly Dictionary<Type, IValueDescriptor> valueDescriptors = new Dictionary<Type, IValueDescriptor>();
        private readonly Dictionary<Type, IObjectDescriptor> objectDescriptors = new Dictionary<Type, IObjectDescriptor>();
        private readonly Dictionary<Type, ICollectionDescriptor> collectionDescriptors = new Dictionary<Type, ICollectionDescriptor>();
        private readonly Dictionary<Type, IArrayDescriptor> arrayDescriptors = new Dictionary<Type, IArrayDescriptor>();
        private readonly Dictionary<string, Type> types = new Dictionary<string, Type>();
        private readonly Dictionary<Type, string> typeNames = new Dictionary<Type, string>();

        public TypeResolverCache(ITypeResolver underlyingTypeResolver)
        {
            UnderlyingTypeResolver = underlyingTypeResolver;
        }

        public ITypeResolver UnderlyingTypeResolver
        {
            get;
            private set;
        }

        public TypeCategory GetTypeCategory(Type type)
        {
            TypeCategory typeCategory;
            if (typeCategories.TryGetValue(type, out typeCategory))
            {
                return typeCategory;
            }

            typeCategory = UnderlyingTypeResolver.GetTypeCategory(type);
            typeCategories[type] = typeCategory;
            return typeCategory;
        }

        public IValueDescriptor GetValueDescriptor(Type type)
        {
            IValueDescriptor descriptor;
            if (valueDescriptors.TryGetValue(type, out descriptor))
            {
                return descriptor;
            }

            descriptor = UnderlyingTypeResolver.GetValueDescriptor(type);
            valueDescriptors[type] = descriptor;
            return descriptor;
        }

        public IObjectDescriptor GetObjectDescriptor(Type type)
        {
            IObjectDescriptor descriptor;
            if (objectDescriptors.TryGetValue(type, out descriptor))
            {
                return descriptor;
            }

            descriptor = UnderlyingTypeResolver.GetObjectDescriptor(type);
            objectDescriptors[type] = descriptor;
            return descriptor;
        }

        public IArrayDescriptor GetArrayDescriptor(Type type)
        {
            IArrayDescriptor descriptor;
            if (arrayDescriptors.TryGetValue(type, out descriptor))
            {
                return descriptor;
            }

            descriptor = UnderlyingTypeResolver.GetArrayDescriptor(type);
            arrayDescriptors[type] = descriptor;
            return descriptor;
        }

        public ICollectionDescriptor GetCollectionDescriptor(Type type)
        {
            ICollectionDescriptor descriptor;
            if (collectionDescriptors.TryGetValue(type, out descriptor))
            {
                return descriptor;
            }

            descriptor = UnderlyingTypeResolver.GetCollectionDescriptor(type);
            collectionDescriptors[type] = descriptor;
            return descriptor;
        }

        public bool TryGetType(string name, out Type type)
        {
            if (types.TryGetValue(name, out type))
            {
                return type != null;
            }

            if (!UnderlyingTypeResolver.TryGetType(name, out type))
            {
                type = null;
            }

            types[name] = type;
            return type != null;
        }

        public string GetTypeName(Type type)
        {
            string name;
            if (typeNames.TryGetValue(type, out name))
            {
                return name;
            }

            name = UnderlyingTypeResolver.GetTypeName(type);
            typeNames[type] = name;
            return name;
        }
    }
}